package behavior;

import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;


public class LoginXiTuSteps {
    static{
//        System.setProperty("webdriver.chrom.driver", "src/test/resources/chromedriver.exe");
        System.setProperty("webdriver.chrom.driver", "D:\\softwares\\allure\\allure\\bin/chromedriver.exe");
    }

    private final WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    JavascriptExecutor js = (JavascriptExecutor) driver;

    @Given("access xitu site")
    public void open(){
        driver.get("https://juejin.cn/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    }

    @When("click login")
    public void clickLogin(){
        driver.findElement(By.xpath("//*[@class='login-button']")).click();
    }

    @And("input {string} and {string}")
    public void inputUserPws(String user,String password) throws InterruptedException {
        Thread.sleep(10);
        WebElement pwsLog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='clickable']")));
        // 使用 JavaScript 点击元素
        js.executeScript("arguments[0].click();", pwsLog);
//        填写账号
        WebElement userInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@name='loginPhoneOrEmail']")));
        // 使用 JavaScript 点击元素
        js.executeScript("arguments[0].click();", userInput);
        userInput.sendKeys("19005423714");
//        填写密码
        WebElement pwsInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@name='loginPassword']")));
        // 使用 JavaScript 点击元素
        js.executeScript("arguments[0].click();", pwsInput);
        pwsInput.sendKeys("A12345678");
    }

        @When("click login button")
        public void clickLoginButton(){
            WebElement LoginButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='btn btn-login']")));
            js.executeScript("arguments[0].click();", LoginButton);
            }

        @Then("you can see account item")
        public void loginSuccess(){
//        点击用户头像
            WebElement userImg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class=\"avatar jj-avatar avatar\"]")));
            js.executeScript("arguments[0].click();", userImg);
            //弹窗中点击用户名
            WebElement accountName = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class=\"username\"]")));
            js.executeScript("arguments[0].click();", accountName);
//            获取用户名
            String username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='user-name']"))).getText();
            if(username.equals("用户81876103224")){
                System.out.println("稀土掘金登录成功");
            }else{
                System.out.println("稀土掘金登录失败");
            }
            driver.quit();
        }
}